-
Notifications
You must be signed in to change notification settings - Fork 681
fix(sahi): Fix Polygon Repair and Empty Polygon Issues #1118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Added `repair_polygon` and `repair_multipolygon` functions to repair invalid polygons and multipolygons. - Implemented `coco_segmentation_to_shapely` function to convert COCO format segmentation data into Shapely objects. - Enhanced the `get_union_polygon` function to handle empty polygons using the newly implemented conversion and repair methods.
- 在 slice_image 和 SliceDataset 中添加 inference_org_image 参数- 如果 inference_org_image 为 True,将原图添加到裁剪列表中 - 更新函数文档和注释以支持新功能
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Overview
This PR fixes issues related to invalid and empty polygons in SAHI by adding repair functions and converting COCO segmentation data into valid Shapely objects. It also enhances the union logic in mask processing and adds an inference image flag to the slicing functions.
- Added repair_polygon, repair_multipolygon, and coco_segmentation_to_shapely functions for polygon corrections.
- Updated get_merged_mask to handle empty polygons using the new conversion methods.
- Introduced an inference_org_image parameter to slicing functions to optionally include the original image in the output.
Reviewed Changes
File | Description |
---|---|
sahi/postprocess/utils.py | Added functions to repair invalid polygons and update union mask logic. |
sahi/slicing.py | Added and propagated the inference_org_image parameter in slicing logic. |
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (2)
sahi/slicing.py:89
- [nitpick] Consider using consistent naming for coordinate variables (e.g., use 'x_max' instead of 'xmax') to maintain clarity across the code.
xmax = min(image_width, x_max)
sahi/postprocess/utils.py:245
- [nitpick] Consider removing or simplifying decorative comment markers to maintain a clean and professional code style.
###################################################sunhao###################################################
@@ -64,6 +65,81 @@ def tolist(self): | |||
else: | |||
return self.list | |||
|
|||
###################################################sunhao################################################### |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider removing or simplifying decorative comment markers to maintain a clean and professional code style.
###################################################sunhao################################################### | |
# Polygon repair functions |
Copilot uses AI. Check for mistakes.
@SunHao-AI can you please fix the failing tests 🤗 |
def coco_segmentation_to_shapely(
segmentation: Union[List, List[List]]
):
"""
Fix segment data in COCO format
:param segmentation: segment data in COCO format
:return:
"""
if isinstance(segmentation, List) and all([not isinstance(seg, List) for seg in segmentation]):
segmentation = [segmentation]
elif isinstance(segmentation, List) and all([isinstance(seg, List) for seg in segmentation]):
pass
else:
raise ValueError("segmentation must be list or list[list]")
polygon_list = []
for coco_polygon in segmentation:
point_list = list(zip(coco_polygon[::2], coco_polygon[1::2]))
shapely_polygon = Polygon(point_list)
polygon_list.append(repair_polygon(shapely_polygon))
shapely_multipolygon = repair_multipolygon(MultiPolygon(polygon_list))
return shapely_multipolygon |
ok |
@SunHao-AI, if you are already at it, you might just make the fix yourself, as you already posted the solution 😄 |
- 从 requirements.txt 中删除了 opencv-python 包 - 此更改可能是为了简化项目依赖或避免特定版本冲突
# Conflicts: # requirements.txt
Co-authored-by: fatih akyon <[email protected]>
- 将原始的 isinstance检查从 'list' 更改为 'List',以符合类型注解的最佳实践 - 保持了原有的逻辑结构,没有引入新的功能或改变现有行为
- 为 image_predictions.masks 添加默认值,避免空指针异常 - 优化掩码 resizing 逻辑,使用原始图像尺寸- 重构部分代码,提高可读性和维护性
- 添加了 get_letter_args 函数,用于计算 resize 和 pad 尺寸 - 优化了对原始预测结果的处理逻辑
- 在将 bool_mask 转换为 COCO格式分割结果时,添加了缺失的步骤 - 先将 bool_mask 缩放回原始尺寸,然后使用 get_coco_segmentation_from_bool_mask 函数进行转换- 修复了 previously 逻辑中未完成的转换过程
- 优化了掩码预测的缩放逻辑,提高了准确性 - 移除了不必要的条件判断和打印语句,简化了代码 - 增加了对 full_shape_list[-1] 的
- 修改了对原始图像尺寸的检查方式,以确保正确的尺寸比较 - 优化了代码结构,提高了可读性和性能
- 移除了对 letter_box 的逆操作,简化了掩码缩放流程 - 统一使用 cv2.resize 对掩码进行缩放,提高了代码可读性 - 删除了不必要的条件判断,减少了代码复杂度
- 删除了用于逆 letter_box 的注释代码 - 保留了原始逻辑,即直接调整 bool_mask 大小以匹配原始图像尺寸
- 新增代码以处理原图推理时的 letterbox 逆操作- 修复了在 sahi 新版本中可能导致推理失败的问题 - 优化了图像尺寸计算逻辑, 提高代码可读性
- 在 Ultralytics 模型中,修复了使用原图进行推理时出现的 letterbox 问题 - 调整了 bool_mask 的处理逻辑,确保在不同形状的图像上进行推理时能够正确处理边界框和掩码
-重构了 sahi/predict.py 中的预测流程,使用原始模型进行推理 - 修复了 sahi/models/ultralytics.py 中的 mask 处理问题 -移除了不必要的注释和冗余代码 - 优化了 letterbox 处理逻辑
-将 image_predictions.data 改为 image_predictions.boxes.data 以避免属性错误 - 此修改确保在没有掩码预测的情况下,程序不会抛出属性错误异常
The first checks fail because of the type annotation, i.e. |
- 调整 POSTPROCESS_NAME_TO_CLASS 字典格式,提高可读性 -移除未使用的 logger 对象- 修复对象预测列表推导式中的缩进问题 - 在模型推理时添加 verbose 参数控制输出 - 优化视频帧或图像迭代的代码结构
- 添加对无效 CUDA 设备索引的处理 - 改进代码注释,增加中文注释以提高可读性 - 保留原有函数功能的同时,提升了代码质量和健壮性
repair_polygon
andrepair_multipolygon
functions to repair invalid polygons and multipolygons.coco_segmentation_to_shapely
function to convert COCO format segmentation data into Shapely objects.get_union_polygon
function to handle empty polygons using the newly implemented conversion and repair methods.